package com.nf.handles;

import com.mysql.cj.protocol.Resultset;
import com.nf.MiniResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;

public class MiniScalarHandler<T> implements MiniResultSetHandler<T> {
    private String columnName=null;
    private int columnIndex=1;

    public MiniScalarHandler() {
    }

    public MiniScalarHandler(String columnName) {
        this.columnName = columnName;
    }

    public MiniScalarHandler(int columnIndex) {
        this.columnIndex = columnIndex;
    }

    public MiniScalarHandler(String columnName, int columnIndex) {
        this.columnName = columnName;
        this.columnIndex = columnIndex;
    }

    @Override
    public T handle(ResultSet rs) throws SQLException {
        if (rs.next()){
            if (columnName!=null){
                return(T) rs.getObject(columnName);
            }
            return(T) rs.getObject(columnIndex);
        }
        return null;
    }
}
